package cs.asiainfo.config.service.impl;

import cs.asiainfo.config.dao.CardDao;
import cs.asiainfo.config.dao.impl.CardDaoImpl;
import cs.asiainfo.config.entity.CardEntity;
import cs.asiainfo.config.entity.MemberEntity;
import cs.asiainfo.config.service.CardService;
import cs.asiainfo.config.service.MemberService;

import java.sql.Date;

/**
 * Created by many on 2016/8/10.
 * @author many
 */
public class CardServiceImpl implements CardService {
    private final static CardDao cardDao = new CardDaoImpl();
    private final static MemberService memberService = new MemberServiceImpl();

    /**
     *新增会员卡
     */
    public String createCard(String cardType, String memberName) throws Exception{
        String message = "";

        MemberEntity memberEntity = memberService.qryMember(memberName);
        if (memberEntity == null){
            message = "Member does not exist!";
            return message;
        }
        CardEntity result = cardDao.qryCard(cardType, memberEntity.getMemberId());
        if (result != null){
            message = "Card already existed!";
            return message;
        }

        CardEntity cardEntity = new CardEntity();
        cardEntity.setStartTime(new Date(System.currentTimeMillis()));
        if (cardType.equals("年卡")){
            cardEntity.setMemberId(memberEntity.getMemberId());
            cardEntity.setCardType("y");
            cardEntity.setLastTime(1);
            cardDao.createCard(cardEntity);
            message = "OK. A new " + cardType + " added to " + memberName;
        }
        else if(cardType.equals("次卡")){
            cardEntity.setMemberId(memberEntity.getMemberId());
            cardEntity.setCardType("t");
            cardEntity.setLastTime(3);
            cardEntity.setLeftTimes(100);
            cardDao.createCard(cardEntity);
            message = "OK. A new " + cardType + " added to " + memberName + "(" + cardEntity.getLeftTimes() + ")";
        }
        else{
            message = "No such card type!";
        }

        return message;
    }
}
